package com.gm.wholesale.module.main.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.gm.wholesale.R
import com.gm.wholesale.databinding.ViewMenuItemBinding

class MenuItemView : LinearLayout {
    private var menuItemView: ViewMenuItemBinding? = null;
    constructor(context: Context) : super(context) {
        initView(context, null)
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initView(context, attrs)
    }
    private fun initView(context: Context, attrs: AttributeSet?) {
        menuItemView = ViewMenuItemBinding.inflate(LayoutInflater.from(context), this, true);

        if (attrs != null) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.menuView)
            val imageId = a.getResourceId(R.styleable.menuView_menuIcon, 0)
            val text = a.getString(R.styleable.menuView_menuName)
            if (imageId > 0) {
                menuItemView?.image?.setImageResource(imageId)
                menuItemView?.nameTv?.text = text
            }
        }
    }
    fun showMenu(icon: Int, name: String?) {
        if (icon > 0) {
            menuItemView?.image?.setImageResource(icon)
            menuItemView?.nameTv?.text = name
        }
    }
}